package com.chandra.pos.order.web;


import com.chandra.pos.common.web.POSController;
import com.chandra.pos.order.dao.entity.OrderStatus;
import com.chandra.pos.order.dao.entity.ShippingStatus;
import com.chandra.pos.order.model.*;
import com.chandra.pos.order.service.OrderService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.persistence.EntityNotFoundException;

/**
 * Created by Admin on 7/2/14.
 */
@Controller
@RequestMapping("order")
public class OrderController extends POSController {


    private static String PAGE_MODULE = "order";

    Logger logger = LoggerFactory.getLogger(OrderController.class);

    @Autowired
    OrderService orderService;


    public OrderController() {
        this.setTabIndex(5);

    }


    @RequestMapping(value = "/home", method = RequestMethod.GET)
    public String index(OrderModel model) {

        return getPageView(PAGE_MODULE, PAGE_HOME);

    }

    @RequestMapping(value = "/search", method = RequestMethod.GET)
    public
    @ResponseBody
    OrderList search(OrderFilter model) {

        return orderService.search(model);

    }

    @RequestMapping(value = "/new", method = RequestMethod.GET)
    public String newOrder(OrderModel model) {

        return getPageView(PAGE_MODULE, PAGE_NEW);

    }


    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public  @ResponseBody OrderModel saveOrder(OrderModel newOrder) {

        newOrder.setShippingStatus(ShippingStatus.PE);
        newOrder.setStatus(OrderStatus.COM);
        return newOrder = orderService.saveOrder(newOrder);


    }

    @RequestMapping(value = "/saveItem", method = RequestMethod.POST)
    public
    @ResponseBody
    OrderItemModel saveItem(OrderItemModel orderItemModel) {

        return orderService.saveOrderItem(orderItemModel);


    }

    @RequestMapping(value = "getOrderDetails", method = RequestMethod.GET)
    public
    @ResponseBody
    OrderModel getProductDetail(OrderFilter filter) {

        OrderModel employeeModel = new OrderModel();
        try {

            employeeModel = orderService.getOrderDetails(filter);
        } catch (EntityNotFoundException e) {

        }

        return employeeModel;
    }

    @RequestMapping(value = "/listOrderItem", method = RequestMethod.GET)
    public
    @ResponseBody
    OrderItemList listOrderItem(OrderItemModel orderItemModel) {

        return orderService.listOrderItems(orderItemModel);


    }


    @RequestMapping(value = "/editItem", method = RequestMethod.POST)
    public String editItem(OrderItemModel orderItemModel) {

        orderService.saveOrderItem(orderItemModel);


        return getPageView(PAGE_MODULE, PAGE_HOME);

    }

    @RequestMapping(value = "/removeItem", method = RequestMethod.POST)
    public String removeItem(OrderItemModel orderItemModel) {


        orderService.removeOrderItem(orderItemModel);

        return getPageView(PAGE_MODULE, PAGE_HOME);

    }


}
